#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Bootstrap Item App Module

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_45"
__date__    = "2014/05/04"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.idt.acidt import MIKAN_IDT_AC
from mikan.exception import MikanStatusException
from mikan.app import MikanViewApp
from mikan.action.itemaction import MikanItemGetAction
from mikan.action.itemaction import MikanItemQueryAction
from mikan.action.itemaction import MikanItemCreateAction
from mikan.action.itemaction import MikanItemCopyAction
from mikan.action.itemaction import MikanItemRemoveAction
from mikan.action.itemaction import MikanItemUpdateAction
from mikan.action.itemaction import MikanItemSequenceUpAction
from mikan.action.itemaction import MikanItemSequenceRemoveAction
from mikan.action.itemaction import MikanItemMailAction

from mikan.viewlet.bootstrap.item import MikanItemBootstrapInfoViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapQueryViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapResultViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapCreateViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapCopyViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapRemoveViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapUpdateViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapSequenceUpViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapSequenceRemoveViewlet
from mikan.viewlet.bootstrap.item import MikanItemBootstrapMailViewlet

from mikan.view.itemview import MikanItemJSONView
from mikan.view.itemview import MikanItemXMLView
from mikan.view.itemview import MikanItemCSVView
from mikan.view.itemview import MikanItemTextView

from mikan.view.itemview import MikanItemsJSONView
from mikan.view.itemview import MikanItemsXMLView
from mikan.view.itemview import MikanItemsCSVView

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan Contant
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

class MikanItemViewApp(MikanViewApp):

	def get_entity_class(self):
		return self.FEntityClass

	def get_ac(self):
		return self.FAC

	def get_can_ac(self):
		return self.FCanAC

	def get_ac_number(self):
		if self.request.issa:
			return 0
		return 3

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, entityclass, ac, canac):
		super(MikanItemViewApp, self).__init__(r)

		self.FEntityClass = entityclass
		self.FAC = ac
		self.FCanAC = canac

	def prepare(self):
		if not super(MikanItemViewApp, self).prepare():
			return False

		#
		if not self.request.db.ac(self.canac, self.acnumber, self.ac):
			# TODO アクセス権エラー例外
			raise MikanStatusException(self.request, 403)

		return True

	# ----------------------------------------------------
	## Property
	entityclass = ESiPCProperty(get_entity_class, None, None)
	ac = ESiPCProperty(get_ac, None, None)
	canac = ESiPCProperty(get_can_ac, None, None)
	acnumber = ESiPCProperty(get_ac_number, None, None)

class MikanSingleItemViewApp(MikanItemViewApp):

	def get_action_class(self):
		return self.FActionClass

	def get_form_class(self):
		return self.FFormClass

	def get_viewlet_class(self):
		return self.FViewletClass

	def get_view_class(self):
		return self.FViewClass

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, canac, actionclass, formclass, viewletclass):
		super(MikanSingleItemViewApp, self).__init__(r, entityclass, ac, canac)

		self.FActionClass = actionclass
		self.FFormClass = formclass
		self.FViewletClass = viewletclass
		self.FViewClass = viewclass

		self.action = actionclass(r, formclass, self.entityclass)

	def create_viewlet(self, view):
		return self.viewletclass(view.contenttarget)

	# ----------------------------------------------------
	## 処理
	#  @param self クラス自身
	def perform(self):
		view = self.viewclass(self.request)

		# リスト
		result = self.create_viewlet(view)
		result.itemaction= self.action

		view.pagetitle = result.title

		self.view = view
		super(MikanSingleItemViewApp, self).perform()

	# ----------------------------------------------------
	## Property
	actionclass = ESiPCProperty(get_action_class, None, None)
	formclass = ESiPCProperty(get_form_class, None, None)
	viewletclass = ESiPCProperty(get_viewlet_class, None, None)
	viewclass = ESiPCProperty(get_view_class, None, None)

class MikanIndexItemViewApp(MikanItemViewApp):

	def get_where(self):
		return self.FWhere

	def get_viewlet_class(self):
		return self.FViewletClass

	def get_form_class(self):
		return self.FFormClass

	def get_view_class(self):
		return self.FViewClass

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass, viewletclass=MikanItemBootstrapResultViewlet, where=None):
		super(MikanIndexItemViewApp, self).__init__(r, entityclass, ac, MIKAN_IDT_AC.AC_READ)

		self.FWhere = where
		self.FFormClass = formclass
		self.FViewClass = viewclass
		self.FViewletClass = viewletclass

	# ----------------------------------------------------
	## 処理
	#  @param self クラス自身
	def perform(self):
		view = self.viewclass(self.request)

		form = self.formclass(self.entityclass())
		if self.where:
			form.entity.values.update(self.where)

		# アイテム
		items = self.request.db.query(self.entityclass, form.entity)

		# リスト
		iscraete = self.request.db.ac(MIKAN_IDT_AC.AC_WRITE, self.acnumber, self.ac)
		result = self.viewletclass(view.contenttarget, iscraete)
		result.itemform = form
		result.items = items
		result.add_sort_javascript(view.body)

		view.pagetitle = result.title

		# ソート処理

		self.view = view
		super(MikanIndexItemViewApp, self).perform()

	# ----------------------------------------------------
	## Property
	where = ESiPCProperty(get_where, None, None)
	formclass = ESiPCProperty(get_form_class, None, None)
	viewletclass = ESiPCProperty(get_viewlet_class, None, None)
	viewclass = ESiPCProperty(get_view_class, None, None)

class MikanQueryItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass, viewletclass=MikanItemBootstrapQueryViewlet,actionclass=MikanItemQueryAction):
		super(MikanQueryItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_READ,
			actionclass,
			formclass,
			viewletclass,
		)

	def create_viewlet(self, view):
		if self.action.items == None:
			return super(MikanQueryItemViewApp, self).create_viewlet(view)
		else:
			iscraete = self.request.db.ac(MIKAN_IDT_AC.AC_WRITE, self.acnumber, self.ac)
			return MikanItemBootstrapResultViewlet(view.contenttarget, iscraete)

class MikanInfoItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass,viewletclass=MikanItemBootstrapInfoViewlet,actionclass=MikanItemGetAction):
		super(MikanInfoItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_READ,
			actionclass,
			formclass,
			viewletclass,
		)

	def create_viewlet(self, parent):
		iscraete = self.request.db.ac(MIKAN_IDT_AC.AC_WRITE, self.acnumber, self.ac)
		return self.viewletclass(parent.contenttarget, iscraete)

# ------------------------------------------------------------
## Mikan アクション 抽象 クラス
class MikanCreateItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass, viewletclass=MikanItemBootstrapCreateViewlet,actionclass=MikanItemCreateAction):
		super(MikanCreateItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_WRITE,
			actionclass,
			formclass,
			viewletclass,
		)

# ------------------------------------------------------------
## Mikan アクション 抽象 クラス
class MikanCopyItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass, viewletclass=MikanItemBootstrapCopyViewlet,actionclass=MikanItemCopyAction):
		super(MikanCopyItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_ACTION_WRITE,
			actionclass,
			formclass,
			viewletclass,
		)

# ------------------------------------------------------------
## Mikan アクション 抽象 クラス
class MikanRemoveItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass, viewletclass=MikanItemBootstrapRemoveViewlet,actionclass=MikanItemRemoveAction):
		super(MikanRemoveItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_ACTION_WRITE,
			actionclass,
			formclass,
			viewletclass,
		)

# ------------------------------------------------------------
## Mikan アクション 抽象 クラス
class MikanUpdateItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass,viewletclass=MikanItemBootstrapUpdateViewlet,actionclass=MikanItemUpdateAction):
		super(MikanUpdateItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_WRITE,
			actionclass,
			formclass,
			viewletclass,
		)

# ------------------------------------------------------------
## Mikan アクション 抽象 クラス
class MikanSequenceUpItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass,viewletclass=MikanItemBootstrapSequenceUpViewlet,actionclass=MikanItemSequenceUpAction):
		super(MikanSequenceUpItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_WRITE,
			actionclass,
			formclass,
			viewletclass,
		)

# ------------------------------------------------------------
## Mikan アクション 抽象 クラス
class MikanSequenceRemoveItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass,viewletclass=MikanItemBootstrapSequenceRemoveViewlet,actionclass=MikanItemSequenceRemoveAction):
		super(MikanSequenceRemoveItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_WRITE,
			actionclass,
			formclass,
			viewletclass,
		)

# ------------------------------------------------------------
## Mikan アクション 抽象 クラス
class MikanMailItemViewApp(MikanSingleItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, viewclass, entityclass, ac, formclass,viewletclass=MikanItemBootstrapMailViewlet,actionclass=MikanItemMailAction):
		super(MikanMailItemViewApp, self).__init__(
			r,
			viewclass,
			entityclass,
			ac,
			MIKAN_IDT_AC.AC_ACTION_READ,
			actionclass,
			formclass,
			viewletclass,
		)

class MikanExportItemViewApp(MikanItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, entityclass, ac, formclass):
		super(MikanExportItemViewApp, self).__init__(r, entityclass, ac, MIKAN_IDT_AC.AC_ACTION_READ)
		self.action = MikanItemGetAction(r, formclass, self.entityclass)

	# ----------------------------------------------------
	## 処理
	#  @param self クラス自身
	def perform(self):
		view = None
		atype = self.request.get_param_str("filetype", "xml")

		if atype == "text":
			view = MikanItemTextView(self.request)
		if atype == "xml":
			view = MikanItemXMLView(self.request)
		if atype == "csv":
			view = MikanItemCSVView(self.request)
		if atype == "json":
			view = MikanItemJSONView(self.request)

		if view == None:
			view = MikanItemXMLView(self.request)

		# リスト
		view.itemaction = self.action

		self.view = view
		super(MikanExportItemViewApp, self).perform()


#
class MikanExportsItemViewApp(MikanItemViewApp):

	# ----------------------------------------------------
	## コンストラクタ
	#  @param self クラス自身
	#  @param r リクエスト
	def __init__(self, r, entityclass, ac, formclass):
		super(MikanExportsItemViewApp, self).__init__(r, entityclass, ac, MIKAN_IDT_AC.AC_ACTION_READ)
		self.action = MikanItemQueryAction(r, formclass, self.entityclass)

	# ----------------------------------------------------
	## 処理
	#  @param self クラス自身
	def perform(self):
		view = None
		atype = self.request.get_param_str("filetype", "xml")

		if atype == "xml":
			view = MikanItemsXMLView(self.request)
		if atype == "csv":
			view = MikanItemsCSVView(self.request)
		if atype == "json":
			view = MikanItemsJSONView(self.request)

		if view == None:
			view = MikanItemsXMLView(self.request)

		# リスト
		view.itemaction =self.action

		self.view = view
		super(MikanExportsItemViewApp, self).perform()

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------

# ------------------------------------------------------------
